package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseFormat;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.entity.SysBaseShopFormat;
import com.maoye.ossp.sys.base.entity.SysBaseShopRegion;
import com.maoye.ossp.sys.base.service.SysBaseFormatService;
import com.maoye.ossp.sys.base.service.SysBaseShopRegionService;
import com.maoye.ossp.sys.base.service.SysBaseShopService;
import com.maoye.ossp.sys.base.utils.SysBaseConstant;
/**
 * 门店controller类
 * @author 梁涛
 * @date   2014-04-21
 */
@Controller
@RequestMapping(value = "sys/base/shop/")
public class SysBaseShopController extends BaseController{
	
	@Resource
	private SysBaseShopService sysBaseShopService ;
	
	@Resource
	private SysBaseFormatService  sysBaseFormatService;
	/**
	 * 获得门店列表
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getShopList")
	public String getShopList(HttpServletRequest request, HttpServletResponse response,Model model,SysBaseShop shop) {
		QueryFilter queryFilter = getQuerFilter(request,shop);
		Page<SysBaseShop> page = (Page<SysBaseShop>)sysBaseShopService
				.find(queryFilter);
		List<SysBaseShop>list =page.getList();
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i=0;i<list.size();i++){	
			map.put("code",list.get(i).getRegionCode());
			list.get(i).setRegionArea(sysBaseShopService.getRegionNameByShopId(map));
		} 
		page.setList(list);
		SysBaseShop sysBaseShop=new SysBaseShop();
		model.addAttribute("sysBaseShop",sysBaseShop);
		model.addAttribute("page", page);
		return resultPage("/sys/base/shop/getShopList", model, request);
	}
	
	/**
	 * 根据区域Id加载门店信息
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="getShopsByPId")
	public void  getShopsByPId (HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String contextPath=request.getContextPath();
		String pId=request.getParameter("treeId");
		StringBuilder json = new StringBuilder();
		Map map = new HashMap();
		map.put("pid", pId);
		List<SysBaseShop>shops = sysBaseShopService.getShopsByPId(map);
		json.append("[");
		for(int i = 0;i<shops.size();i++){
			SysBaseShop shop = shops.get(i);
			json.append("{").append("id:").append(shop.getNo()).append(",");
			json.append("pId:").append(shop.getRegionCode()).append(",");
			json.append("name:\"").append(shop.getName()).append("\"");//.append(",");
			json.append("}");
			if(i < shops.size()-1){
				json.append(",");
			}		
		}
		json.append("]");
		out.print(json.toString());
		out.flush();
		out.close();
	}
	@RequestMapping(value ="addShop")
	public String addShop(HttpServletRequest request, HttpServletResponse response,Model model){
		String regionCode =request.getParameter("treeid");
		String regionName=request.getParameter("name");
		SysBaseShop sysBaseShop=new SysBaseShop();
		sysBaseShop.setRegionCode(regionCode);
		model.addAttribute("sysBaseShop",sysBaseShop);
		model.addAttribute("regionName",regionName);
		return "/sys/base/shop/add"; 
	}
	@RequestMapping(value ="checkShopCode")
	public void checkShopCode(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseShopService.checkShopCode(map)){
			json.put("resCode",0); //门店号可用
		}else{
			json.put("resCode",1);//门店号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	@RequestMapping(value ="saveShop")
	public String  saveShop(SysBaseShop shop,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		shop.setId(idGenerator.getUId());
		shop.setNo(shop.getNo().replaceAll(",","").toString());
		shop.setRegionCode(shop.getRegionCode().replaceAll(",","").toString());
		shop.setPostId(shop.getPostId().replace("|post","").toString());
		shop.setShopManager(shop.getShopManager().replace("|user","").toString());  
		shop.setContractManager(shop.getContractManager().replace("|user","").toString());  
		String formatsCode=shop.getFormatCode();
		String []formatCode=formatsCode.split(";");
		SysBaseShopFormat  sysBaseShopFormat=new SysBaseShopFormat();
		sysBaseShopFormat.setShopCode(shop.getNo());
		if(formatCode!=null){
		  for(int i=0;i<formatCode.length;i++){
			  sysBaseShopFormat.setId(idGenerator.getUId());
			  sysBaseShopFormat.setFormatCode(formatCode[i].toString());
			  sysBaseFormatService.saveShopFormatRelation(sysBaseShopFormat);
		  }
		}
		//设置创建人
		shop.setCreateBy("");
		sysBaseShopService.saveShop(shop);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getShopList";
	}
	/**
	 * 查看门店信息
	 * @param region
	 * @param model
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping(value ="viewShop")
	public String viewShop(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String parentArea =request.getParameter("parentArea");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseShop sysBaseShop=null;
		sysBaseShop=sysBaseShopService.getShopById(map);
		if(sysBaseShop!=null){
			//添加父区域
			sysBaseShop.setRegionArea(parentArea);
			List<SysBaseFormat>list=sysBaseFormatService.getShopFormatRelationList(map);
			String formatsName="";
			if(list!=null){
				for(int i=0;i<list.size();i++){
					formatsName=formatsName+list.get(i).getName();
					if(i<list.size()-1){
						formatsName=formatsName+";";
					}
				}
			}
			sysBaseShop.setFormatName(formatsName);
			model.addAttribute("shop",sysBaseShop);
		}
		return "/sys/base/shop/view";
	}
	@RequestMapping(value ="updateShop")
	public String updateShop(HttpServletRequest request,HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String regionArea =request.getParameter("parentArea");
		SysBaseShop shop=null;
		Map map = new HashMap();
		map.put("code",code);
		shop = sysBaseShopService.getShopById(map);
		String cityName=shop.getCityName();
		String formatCode="";
		String formatName="";
		String postName=shop.getPostName();
		String shopManagerName=shop.getShopManagerName();
		String contractManagerName=shop.getContractManagerName();
		//regionArea=sysBaseShopService.getRegionNameByShopId(map);
		List<SysBaseFormat>list=sysBaseFormatService.getShopFormatRelationList(map);
		for(int i=0;i<list.size();i++){
			formatCode=formatCode+list.get(i).getCode();
			formatName=formatName+list.get(i).getName();
			if(i<list.size()-1){
				formatCode=formatCode+";";
				formatName=formatName+";";
			}
		}
		shop.setFormatCode(formatCode);
		if(formatName!=null){
			model.addAttribute("formatName",formatName);
		}
		if(regionArea!=null){
			model.addAttribute("regionArea",regionArea);
		}
		if(cityName!=null){
			model.addAttribute("cityName",cityName);
		}
		if(postName!=null){
			model.addAttribute("postName",postName);
		}
		if(shopManagerName!=null){
			model.addAttribute("shopManagerName",shopManagerName);
		}
		if(contractManagerName!=null){
			model.addAttribute("contractManagerName",contractManagerName);
		}
		model.addAttribute("sysBaseShop",shop);
		return "/sys/base/shop/update";
	}
	@RequestMapping(value ="saveUpdateShop")
	public String saveUpdateShop(HttpServletRequest request,HttpServletResponse response,SysBaseShop shop ,RedirectAttributes redirectAttributes){
		shop.setId(idGenerator.getUId());
		//设置修改人
		shop.setUpdateBy("");
		shop.setPostId(shop.getPostId().replace("|post","").toString());
		shop.setShopManager(shop.getShopManager().replace("|user","").toString());
		shop.setContractManager(shop.getContractManager().replace("|user","").toString());
		Map map=new HashMap();
		map.put("shopCode",shop.getNo());
		sysBaseFormatService.removeShopFormatRelationByShopCode(map);
		String formatsCode=shop.getFormatCode();
		String []formatCode=formatsCode.split(";");
		SysBaseShopFormat  sysBaseShopFormat=new SysBaseShopFormat();
		sysBaseShopFormat.setShopCode(shop.getNo());
		if(formatCode!=null){
		  for(int i=0;i<formatCode.length;i++){
			  sysBaseShopFormat.setId(idGenerator.getUId());
			  sysBaseShopFormat.setFormatCode(formatCode[i].toString());
			  sysBaseFormatService.saveShopFormatRelation(sysBaseShopFormat);
		  }
		}
		
		sysBaseShopService.saveUpdateShop(shop);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getShopList";
	}
	@RequestMapping(value ="deleteShop")
	public String deleteShop(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		map.put("shopCode",code);
		sysBaseFormatService.removeShopFormatRelationByShopCode(map);
		sysBaseShopService.deleteShop(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getShopList";
	}
}
